<?php

namespace app\index\traits;

use app\common\dao\SettingDao;
use app\common\model\ShareableLawyerInterface;
use app\common\model\Trade;

/**
 * 订单分账金额预估
 * Trait TradeShareEstimateTrait
 * @package app\index\traits
 */
trait TradeShareEstimateTrait
{

    /**
     * 预估律师佣金
     * @param $money
     * @param ShareableLawyerInterface $shareable
     * @return float
     */
    private function estimateLawyerMoney($money, $settleStatus, ShareableLawyerInterface $shareable)
    {
        // 不结算的订单，佣金是0
        if ($settleStatus === Trade::NOTSETTLE) {
            return 0;
        }

        $lawyerShareModel = $shareable->lawyerShareModel;
        if ($lawyerShareModel == 0) {
            // 百分比

            $shareRate = $shareable->lawyerShareRate;
            if (!is_numeric($money) || !is_numeric($shareRate) || $shareRate < 0) {
                return 0;
            }

            return round($money * $shareRate, 2);
        } elseif ($lawyerShareModel == 1) {
            // 固定金额

            return $shareable->lawyerShareMoney;
        } elseif ($lawyerShareModel == 2) {
            // 统一调价

            $commission = $this->getSettingDao()->getValueByKey('commission', null);
            if (is_numeric($commission)) {
                $commission = round($commission, 2);
                if (bccomp($commission, 0) < 0) {
                    $commission = 0;
                }

                return $commission;
            }
        }

        return 0;
    }

    private function getSettingDao(): SettingDao
    {
        return app(SettingDao::class);
    }
}